<?php

/**
 * user actions.
 *
 * @package    Librarie
 * @subpackage user
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class userActions extends sfActions
{
	public function executeCumpara(sfWebRequest $request)
	{
		$comanda = new Comanda();
		$currentUser = $this->getUser()->getAttribute('user');
		$comanda->saveComanda($this->getUser()->getAttribute('comanda'), $currentUser);
		$this->getUser()->deleteComanda();
		$this->sendEmail($currentUser);
		$this->setTemplate('cumpara');
	}

	private function sendEmail(User $user)
	{
		$message = $this->getMailer()->compose
		(
		array('claudia.iosipescu@gmail.com' => 'Libraria Anonimus'),
		$user->getEmail(),'Comanda inregistrata',
						"Buna ziua {$user->getNume()},\n
						Comanda dumneavoastra a fost inregistrata. \n 
						Va multumim ca ati cumparat de la noi. \n 
						Va mai asteptam, \n 
						Libraria Anonimus"
		);
		$this->getMailer()->send($message);
	}

	public function executeDeleteFromCos(sfWebRequest $request)
	{
		$id_carte = $request->getParameter('id_carte');
		$this->comenzi = $this->getUser()->getAttribute('comanda',array());
		foreach($this->comenzi as $comanda)
		{
			if($id_carte == $comanda)
			{
				if(($key = array_search($comanda, $this->comenzi)) !== false)
				{
					unset($this->comenzi[$key]);
				}
				break;
			}
		}
		$this->getUser()->setAttribute('comanda', $this->comenzi);
		$this->setTemplate('cos');
	}


	public function executeLogout(sfWebRequest $request)
	{
		//$this->getUser()->setLogged(false);
		$this->getUser()->setUnlogged();
		$this->form = new UserForm();
		$this->setTemplate('login');
	}



	public function executeIndex(sfWebRequest $request)
	{
		$request->setAttribute("login",false);
		$this->users = Doctrine_Core::getTable('User')
		->createQuery('a')
		->execute();
	}


	public function executeShow(sfWebRequest $request)
	{
		//pt sesiuni
		$request->setAttribute("login",false);
		$this->user = Doctrine_Core::getTable('User')->find(array($request->getParameter('id_user')));

		//$this->user = $this->getRoute()->getObject();

		//$this->getUser()->addUserNameToSession($this->user);

		$this->forward404Unless($this->user);

	}



	public function executeLogin(sfWebRequest $request)
	{
		if($this->getUser()->getCurrentUser()==NULL)
		{
			$request->setAttribute("login",true);
			$this->form = new UserForm();
		}
		else
		{
			$this->setTemplate('index','Carte');
		}
	}

	public function executeLoginDone(sfWebRequest $request)
	{
		$this->form = new UserForm();

		$this->form->bind($request->getParameter($this->form->getName()), $request->getFiles($this->form->getName()));

		if($this->form->isValid())
		{
			$a = $request->getParameter('user_name');
			$b = $request->getParameter('pws');

			$q = Doctrine_Query::create()->from('User u')->where('u.user_name = ?', $a);
			$user = $q->execute();

			if($user[0] != NULL)
			{
				if($user[0]->getPws() != $b)
				{
					$this->form = new UserForm();
					$this->setTemplate('login');
				}
				else
				{
					$tip = $user[0]->getIdTip();

					$qq = Doctrine_Query::create()->from('TipUser tu')->where('tu.id_tip = ?', $tip);
					$tipUtilizator = $qq->execute();
						
					if ($tipUtilizator[0]->getNumeTip() == 'Gestionar')
					{
						$this->redirect("http://localhost:8080/backend_dev.php/");
					}
					else
					{
						$this->getUser()->addUserNameToSession($user[0]);
						$this->setTemplate('userLoged');
						return;
					}
				}
			}
		}

		$this->setTemplate('login');
	}

	public function executeNew(sfWebRequest $request)
	{
		$request->setAttribute("login",false);
		$this->form = new NewaccountForm();
		//$this->setTemplate('new');
	}

	public function executeCreate(sfWebRequest $request)
	{
		$request->setAttribute("login",false);
		$this->forward404Unless($request->isMethod(sfRequest::POST));

		$this->form = new NewaccountForm();

		$this->processForm($request, $this->form);

		$this->setTemplate('new');
	}

	public function executeEdit(sfWebRequest $request)
	{
		$request->setAttribute("login",false);
		//$this->forward404Unless($user = Doctrine_Core::getTable('User')->find(array($request->getParameter('id_user'))), sprintf('Object user does not exist (%s).', $request->getParameter('id_user')));
		$username = $this->getUser()->getCurrentUser()->getUserName();
		$user = Doctrine_Query::create()->from('User u')->where('u.user_name = ?', $username)->execute();//Doctrine_Core::getTable('User')->find($username);
		$this->form = new NewaccountForm($user[0]);

		$this->comenzi = $user[0]->getComenziForUser();
		$this->cartiPeComanda = array();
		foreach($this->comenzi as $comanda)
		{
			$carti = $user[0]->getCartiForComanda($comanda->getIdComanda());
			array_unshift($this->cartiPeComanda,$carti);
		}
	}


	public function executeUpdate(sfWebRequest $request)
	{
		$request->setAttribute("login",false);
		$this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
		$this->forward404Unless($user = Doctrine_Core::getTable('User')->find(array($request->getParameter('id_user'))), sprintf('Object user does not exist (%s).', $request->getParameter('id_user')));
		$this->form = new UserForm($user);

		$this->processForm($request, $this->form);

		$this->setTemplate('edit');
	}


	public function executeDelete(sfWebRequest $request)
	{
		$request->setAttribute("login",false);
		$request->checkCSRFProtection();

		$this->forward404Unless($user = Doctrine_Core::getTable('User')->find(array($request->getParameter('id_user'))), sprintf('Object user does not exist (%s).', $request->getParameter('id_user')));
		$user->delete();

		$this->redirect('user/index');
	}


	public function executeCos(sfWebRequest $request)
	{
		//$this->getUser()->addUserNameToSession($user);

		$this->comenzi = $this->getUser()->getAttribute('comanda',array());
		$this->setTemplate('cos');
	}


	protected function processForm(sfWebRequest $request, sfForm $form)
	{
		$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
		//echo $request->getParameter('id_tip');
		if ($form->isValid())
		{
			$user = $form->save();
			$this->getUser()->addUserNameToSession($user);
			$this->setTemplate('userLoged');
		}
	}



}
